<template>
  <!-- 选择供应商 -->
  <div>
    <div>
      <TopFilter :list="topFilterList" :cols="3" :labelWidth="85" :collapse="false" @filterChange="changeHandle" />
      <FilterTable
        ref="table"
        height="auto"
        :columns="columns"
        columnsRef="partPartPurchasePurchasePlaneProvideSearchHelper"
        :fetchapi="fetchapi"
        :params="fetchParams"
        :datakey="'rows'"
        selectionType="single"
        :onRowSelectChange="selectChangeHandler"
        :onColumnsChange="columns => (this.columns = columns)"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="confirmHandler">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { queryBusinessCustomer } from '@/api/part/partInfoManage/contactsCustomer';

export default {
  name: '',
  data() {
    this.selectedRows = [];
    return {
      topFilterList: this.createTopFilters(),
      columns: this.createTableColumns(),
      fetchapi: queryBusinessCustomer,
      fetchParams: { xhrAbort: false }
    };
  },
  methods: {
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '供应商代码',
          fieldName: 'customerCode',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '供应商名称',
          fieldName: 'customerName',
          placeholder: '请输入...'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '供应商代码',
          dataIndex: 'customerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '供应商名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    changeHandle(val) {
      console.log(val);
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign({ xhrAbort: false }, val);
    },
    selectChangeHandler(rows) {
      this.selectedRows = rows;
    },
    confirmHandler() {
      if (!this.selectedRows.length) {
        return this.$notify({
          title: '提示信息',
          message: '请选择数据！',
          type: 'warning'
        });
        return;
      }
      this.$emit('onConfirm', this.selectedRows[0]);
      this.closeDialog(this.selectedRows[0]);
    },
    closeDialog(row) {
      this.$emit('close');
    }
  }
};
</script>

<style lang="less" scoped></style>
